﻿
using System;
using System.ComponentModel;
using System.Web.Mvc;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core;
using HMIS.Infrastructure.Facade;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;


namespace HMIS.MVC.Controllers
{
	//字典管理功能的Controller
	public class InfraDictionaryController : BaseController
	{
		private readonly IDictionaryFacade DictionaryFacade;
		private readonly IDicCategoryFacade DicCategoryFacade;

		public InfraDictionaryController(IDictionaryFacade DictionaryFacade, IDicCategoryFacade DicCategoryFacade)
		{
			this.DictionaryFacade = DictionaryFacade;
			this.DicCategoryFacade = DicCategoryFacade;
		}


		//这只是个例子说明如何调用Facade
		public ActionResult SaveOrUpdateDictionary()
		{
			var entity = new Dictionary();
			DictionaryFacade.SaveOrUpdate(entity);
			return Content("");
		}

		[Description("获取所有常用字典")]
		public ActionResult GetPlistAll()
		{
			var data = DictionaryFacade.GetPlistAll();
			return this.JsonFormat(
				new ExtGirdData<Dictionary> { data = data },
				new string[] { "Category" });
		}

		[Description("获取字典参数清单")]
		public ActionResult GetPlistCategoryDictionary()
		{
			long total;
			var data = DictionaryFacade.GetPlistByCategoryId(
				Request["Id"],
				int.Parse(Request["start"]),
				int.Parse(Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<Dictionary> { total = total, data = data });
		}

		/// <summary>
		/// 根据字典名获取参数
		/// </summary>
		/// <returns></returns>
		public ActionResult GetPlistByCategoryCode()
		{
			long total;
			string code = Request["Code"];
			var data = DictionaryFacade.GetPlistByCategoryCode(
				Request["Code"],
				Request["query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<Dictionary> { total = total, data = data });
		}

		[ExtResult]
		[Description("判断字典的名称、编码是否在类别下唯一")]
		public ActionResult IsFieldExist()
		{
			var result = DictionaryFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}

		[ExtResult]
		[Description("添加、修改字典参数")]
		public ActionResult SaveOrUpdate()
		{
			Dictionary entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new Dictionary
				{
					Name = Request["Name"],
					Code = Request["Code"],
					InputCode1 = Request["InputCode1"],
					InputCode2 = Request["InputCode2"],
					InputCode3 = Request["InputCode3"],
					Index = int.Parse(Request["Index"]),
					Description = Request["Description"],
					Category = DicCategoryFacade.GetDicCategory(Request["CateId"])
				};
			}
			else
			{
				entity = this.DictionaryFacade.GetDictionary(Request["Id"]);
				entity.Name = Request["Name"];
				entity.Code = Request["Code"];
				entity.InputCode1 = Request["InputCode1"];
				entity.InputCode2 = Request["InputCode2"];
				entity.InputCode3 = Request["InputCode3"];
				entity.Index = int.Parse(Request["Index"]);
				entity.Description = Request["Description"];
				entity.Category = DicCategoryFacade.GetDicCategory(Request["CateId"]);
			}

			DictionaryFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("删除字典")]
		public ActionResult Delete()
		{
			DictionaryFacade.DeleteDictionary(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
	}
}
